<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.beaut.project.util.FlowUtility" %>
<%@ page import="com.beaut.project.util.StaticData" %>
<%@ include file="/public/header.jsp"%>
<%
    String callback = request.getParameter("callback");
	String fb_advances_id = request.getParameter("fb_advances_id") == null ? "" : request.getParameter("fb_advances_id");
	String type = request.getParameter("type") == null ? "" : request.getParameter("type");
	String gd_id = request.getParameter("gd_id") == null ? "" : request.getParameter("gd_id");
	String dept_id = request.getParameter("dept_id") == null ? "" : request.getParameter("dept_id");
	String dept_name = request.getParameter("dept_name") == null ? "" : request.getParameter("dept_name");
	String gd_code = request.getParameter("gd_code") == null ? "" : request.getParameter("gd_code");
	String gd_name = request.getParameter("gd_name") == null ? "" : request.getParameter("gd_name");
	String id = request.getParameter("id") == null ? "" : request.getParameter("id");
	Map<String, Object> sessionMap = (Map<String, Object>) session.getAttribute("LOGIN_USER");
	String person_id = String.valueOf(sessionMap.get("id"));
	String person = String.valueOf(sessionMap.get("truename"));
	boolean isAdd = "".equals(id);
    int flag = 0;
    if(!isAdd){
    	flag = 1;
    }
%>
<html>
	<head>
		<title><%=isAdd?"新建借款单":"编辑借款单" %></title>
			<link href="<%=contextPath%>/public/css/smart_wizard_vertical.css" rel="stylesheet"	type="text/css">
		<script type="text/javascript" src="<%=contextPath%>/public/js/jquery.smartWizard.js"></script>
		<link  rel="stylesheet"  href  ="<%=contextPath%>/public/swfupload/css/swfupload.css">
	
	<script type="text/javascript">
	$(function(){
		$("#table").datagrid({
			title:"分包商垫款",
			iconCls:'icon-zc',
			loadMsg:'正在加载数据，请稍后......',
			url:'<%=contextPath %>/springMVC/gd/listGdAdvances?gd_id=<%=gd_id%>&fb_advances_id=<%=fb_advances_id%>&chose_status=3',
			rownumbers:true,
			fitColumns:true,
			nowrap:false,
			idField:'id',
			frozenColumns:[[
			      {field:'ck',checkbox:true},
			      {title:'主键',field:'id',hidden:true}
			]],
			columns:[[
				{title:'分包商',field:'fb_advances',width:100,align:'center'},
				{title:'申请日期',field:'operation_date',width:100,align:'center',sort:true},
				{title:'垫款金额',field:'money',width:100,align:'center'},
				{title:'已还金额',field:'already_money',width:100,align:'center'},
				{title:'结余本金',field:'on_money',width:100,align:'center'},
				{title:'利率',field:'rate',width:100,align:'center'},
				{title:'放款日期',field:'actual_operation_date',width:100,align:'center',sort:true},
				{title:'经办人',field:'person_name',width:100,align:'center'},
				{title:'摘要',field:'remark',width:200,align:'center'},
				{title:'操作',field:'edit',width:100,align:'center',formatter:function(value,rowData,rowIndex){
                	return "<input type='button' style='border:0px #ff0000 solid;width:16px;height:16px;cursor:pointer;' class='icon-details' title='查看' onclick=\"javascript:viewData('"+rowData.id+"');\"/>";

				}}
			]],
			pageSize:10,
			pageList : [10,50,100],  
			pagination:true,
			toolbar:"#tb",
			onLoadSuccess:function(rtJson){
				if(rtJson.rtState== '1')
				{
					$.messager.alert('提示',rtJson.rtMsrg);
				}
			}
		});
	});
		function  viewData(id){
			openDialogResize("<%=contextPath %>/springViews/gd/addOrEditAdvances.jsp?callback=rtCallBack&isShow=1&gd_id=<%=gd_id%>&dept_id=<%=dept_id%>&dept_name=<%=dept_name%>&gd_code=<%=gd_code%>&gd_name=<%=gd_name%>&id="+id,1000, 800);
		}
		
		function rtCallBack(){
			$('#table').datagrid('reload');
		}
		function choseAdvances(){
			  var rows=$('#table').datagrid('getSelections');
			var date=new Date();
		       
		        var year = date.getFullYear();       //年
		        var month = date.getMonth() + 1;     //月
		        var day = date.getDate();            //日
				var operation_date=year+"-"+month+"-"+day;
			var dates=0;
			if(0==date.getYear()%4&&((date.getYear()%100!=0)||(date.getYear()%400==0))){
				dates=366;
			}else{
				dates=365;
			}
			
			 var rows=$('#table').datagrid('getSelections');
				var ids="";//主键
				var sumMoneys=0;//总金额
				var lxMoneys="";//利息
				var actual_operation_dates="";//放款时间
				var moneys="";//金额
				var fb_advances="";
				var person_names="";
				var yetMoneys="";
				var already_moneys="";
				var rates="";
				var on_moneys="";
				for(var i=0;i<rows.length;i++)
				{
					var dateNum=0;
					var oneTime=operation_date;
					var twoTime=rows[i].actual_operation_date.split(" ");
					dateNum=getDays(oneTime,twoTime[0]);
					var money=parseFloat(rows[i].on_money*rows[i].rate*dateNum/(100*dates)+rows[i].on_money);
					var lxMoney=parseFloat(rows[i].on_money*rows[i].rate*dateNum/(100*dates));//利息
					if(i==(rows.length-1))
					{
						ids+=rows[i].id;
						lxMoneys+=lxMoney;
						moneys+=rows[i].money.toFixed(2);
						yetMoneys+=parseFloat(rows[i].on_money)+parseFloat(lxMoney);
						actual_operation_dates+=rows[i].actual_operation_date;
						fb_advances+=rows[i].fb_advances;
						person_names+=rows[i].person_name;
						rates+=rows[i].rate;
						already_moneys+=rows[i].already_money;
						on_moneys+=rows[i].on_money;
					}else
					{
						ids+=rows[i].id+",";
						lxMoneys+=lxMoney+",";
						moneys+=rows[i].money+",";
						yetMoneys+=parseFloat(rows[i].on_money)+parseFloat(lxMoney)+",";
						actual_operation_dates+=rows[i].actual_operation_date+",";
						fb_advances+=rows[i].fb_advances+",";
						person_names+=rows[i].person_name+",";
						rates+=rows[i].rate+",";
						already_moneys+=rows[i].already_money+",";
						on_moneys+=rows[i].on_money+",";
					}
					
				}
				
				window.opener.addRows(ids,lxMoneys,moneys,actual_operation_dates,fb_advances,person_names,yetMoneys,rates,already_moneys,on_moneys);
				window.close();
		}

		
	</script>
	</head>
	<body>
		<div id="table" data-options="fit:true" ></div>
		<div id="tb" style="padding:3px;height:auto">
			<div align="left">
				<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" plain="true" onclick="choseAdvances();">选中</a>
			</div>
		</div>
		</div>
	
		
	</body>
</html>